<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    
</body>
</html>
<script>
    const users = [
        {id:1, name:"a"},
        {id:2, name:"a"},
        {id:3, name:"b"},
        {id:4, name:"b"}
    ]

    const arr = [
  { id: 1, name: 'aa', age: 16 },
  { id: 1, name: 'aa', age: 16 },
  { id: 3, name: 'cc', age: 20 },
  { id: 3, name: 'cc', age: 20 },
  { id: 2, name: 'bb', age: 18 },
  { id: 1, name: 'aa', age: 16 },
]
// 方法一
// const newArr = users.filter((ele, i, self) => {
//   const index = self.findIndex(item => {
//     return item.name === ele.name
//   })
//   return index === i
// })
// console.log(newArr)
// /*
//     0: {id: 1, name: "a"}
//     1: {id: 3, name: "b"}
//     length: 2
//     __proto__: Array(0)
// */


// 方法二
function unique(users) {
  const res = new Map();
  return users.filter((a) => !res.has(a.name) && res.set(a.name, 1))
}
const newArr = unique(users)
console.log(newArr)


const data = [

    { "name": "衣服", "price": 8.5, "count": 1 },

    { "name": "裤子", "price": 10.2, "count": 2 },

    { "name": "瓜子", "price": 11.0, "count": 5 },

    { "name": "鞋子", "price": 9.4, "count": 1 },

    { "name": "帽子", "price": 6.2, "count": 3 }
]

// var getNewList = data.filter((val,i)=>{
//     console.log(val)
//     return val.price>=8 && val.price<=10
// })
// console.log(getNewList)
// 0: {name: "衣服", price: 8.5, count: 1}
// 1: {name: "鞋子", price: 9.4, count: 1}
// length: 2
// __proto__: Array(0)
var total = 0;
for (var i = 0; i < data.length; i++) {
        total += parseFloat(data[i].price) * data[i].count;
      }
    total = parseFloat(total.toFixed(2));
      console.log(total)   //111.9   


      let bookInfos = [
            {

            id: 'app',
            name: 'js入门',
            parent_id: '',

            },
            {
            id: '1',
            name: '第一章',
            parent_id: 'app',
            },
            {
            id: '2',
            name: '第二章',
            parent_id: 'app',
            },
            {
            id: '1-1',
            name: '第一章第一节',
            parent_id: '1',
            },
            {
            id: '1-2',
            name: '第一章第二节',
            parent_id: '1',
            },
            {
            id: '2-1',
            name: '第二章第一节',
            parent_id: '2',
            },
]


function toTree() {
        // 删除 所有 children,以防止多次调用
        data.forEach(function (item) {
            delete item.children;
        });
 
        // 将数据存储为 以 id 为 KEY 的 map 索引数据列
        var map = {};
        data.forEach(function (item) {
            map[item.id] = item;
        });
//        console.log(map);
        var val = [];
        data.forEach(function (item) {
            // 以当前遍历项，的pid,去map对象中找到索引的id
            var parent = map[item.pid];
            // 好绕啊，如果找到索引，那么说明此项不在顶级当中,那么需要把此项添加到，他对应的父级中
            if (parent) {
                (parent.children || ( parent.children = [] )).push(item);
            } else {
                //如果没有在map中找到对应的索引ID,那么直接把 当前的item添加到 val结果集中，作为顶级
                val.push(item);
            }
        });
        return val;
    }
    console.log(toTree(data))

</script>